package com.mr.base.service;

import java.util.Date;
import java.util.List;

import com.mr.base.constant.MrBaseConstant;
import com.mr.base.dao.CardDao;
import com.mr.base.domain.Card;
import com.mr.base.domain.CardLine;
import com.mr.base.domain.Item;
import com.skysz.framework.action.ActionUtils;
import com.skysz.framework.dao.DomainDao;
import com.skysz.framework.service.AbstractBaseService;
import com.skysz.framework.utils.Result;
import com.skysz.framework.utils.date.DateUtils;

/**
 * 卡管理
 * @author Administrator
 *
 */
public class CardService extends AbstractBaseService<Card>{
	private CardDao bcardDao;
	
	private static final long serialVersionUID = 1L;

	@Override
	public DomainDao<Card> getDomainDao() {
		return bcardDao;
	}

	public void setBcardDao(CardDao bcardDao) {
		this.bcardDao = bcardDao;
	}
	
	/**
	 * 新增
	 * @param t
	 */
	public Result insertBcard(Card t) {
		Result r = new Result();
		r.setCode(Result.CODE_FAILURE);
		
		t.setEnable(true);
		if (t.getEnableDays() != null && t.getEnableDays() >= 1) { 
			t.setEnableDate(DateUtils.nextDays(new Date(), t.getEnableDays()));
		}
		//保存行表
		List<String[]> lineList = t.getItemLineList();
		
		//保存头表
		this.bcardDao.insert(t);
		
		if (lineList != null && lineList.size()>=1) {
			CardLine bl = null;
			Card b = null;
			for (String[] s : lineList) {
				bl = initGridLine(t.getType(), s);
				bl.setEnable(true);
				//卡
				b = new Card(t.getId());
				bl.setCard(b);
				bl.setCard(b);
				bl.setOrg(ActionUtils.getCurrentOrg()) ;
				this.utilsDao.insert(bl);
			}
		}
		
		r.setCode(Result.CODE_SUCCESS);
		return r;
	}
	
	private CardLine initGridLine(String type, String[] s) {
		CardLine bl = new CardLine();
		ActionUtils.setWho(bl);
		bl.setEnable(true);
		
		if (type.equals(MrBaseConstant.CardType.ITEM)) {
			//项目
			Item it = new Item();
			it.setId(Long.parseLong(s[5]));
			bl.setItem(it);
			//原价
			bl.setInitPrice(Double.parseDouble(s[1]));
			//现价
			bl.setNowPrice(Double.parseDouble(s[2]));
			//次数
			bl.setCount(Integer.parseInt(s[3]));
		} else if (type.equals(MrBaseConstant.CardType.LOCAL)) {
			//项目
			Item it = new Item();
			it.setId(Long.parseLong(s[2]));
			bl.setItem(it);
		}
		return bl;
	}
	
	/**
	 * 修改
	 * @param t
	 */
	public Result updateBcard(Card t) {
		Result r = new Result();
		r.setCode(Result.CODE_FAILURE);
		
		//保存行表
		List<String[]> lineList = t.getItemLineList();
		
		Card findB = this.bcardDao.get(t.getId());
		findB.setStatus(t.getStatus());
		findB.setName(t.getName());
		findB.setInitPrice(t.getInitPrice());
		findB.setFreePrice(t.getFreePrice());
		findB.setIsIntegral(t.getIsIntegral());
		findB.setDefaultIntegralValue(t.getDefaultIntegralValue());
		findB.setEnableDays(t.getEnableDays());
		if (findB.getEnableDays() != null && findB.getEnableDays() >= 1) { 
			findB.setEnableDate(DateUtils.nextDays(new Date(), findB.getEnableDays()));
		}
		findB.setNow4Count(t.getNow4Count());
		findB.setNow4Price(t.getNow4Price());
		ActionUtils.setWho(findB, false);
		
		//修改头信息
		this.bcardDao.update(findB);
		
		//插入
		if (lineList != null && lineList.size()>=1) {
			//先删除行
			this.bcardDao.deleteBcardLine(t.getId());
			
			CardLine bl = null;
			Card b = null;
			for (String[] s : lineList) {
				bl = initGridLine(t.getType(), s);
				bl.setEnable(true);
				//卡
				b = new Card();
				b.setId(t.getId());
				bl.setCard(b);
				bl.setCard(b);
				bl.setOrg(ActionUtils.getCurrentOrg()) ;
				this.utilsDao.insert(bl);
			}
		}
		
		r.setCode(Result.CODE_SUCCESS);
		return r;
	}
	
	/**
	 * 禁用
	 * @param id
	 */
	public void disableBcard(Long id) {
		this.bcardDao.disableBcard(id);
	}
	
	/**
	 * 加载卡信息
	 * @param id
	 * @return
	 */
	public Card loadBcard(Long id) {
		return this.bcardDao.loadBcard(id);
	}
	
	/**
	 * 查找卡行信息
	 * @param id
	 * @param type
	 * @return
	 */
	public List<Object[]> findCardLine(Long id, String type) {
		return this.bcardDao.findCardLine(id, type);
	}
}
